package com.example.springbootsecurityjwt.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.springbootsecurityjwt.common.ResponseResult;
import com.example.springbootsecurityjwt.service.UserService;
import com.example.springbootsecurityjwt.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @program: SpringBoot-Security-JWT
 * @description: 用户接口
 * @author: Coder_Pan
 * @create: 2022-03-22 10:33
 **/
@RestController
@RequestMapping("/api/user")
public class UserController {
    final
    UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }


    /**注册接口
     * POST 新增
     *
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public ResponseResult<?> addUser(@RequestParam("userName") String userName,
                                     @RequestParam("password") String passWord,
                                     @RequestParam("password2") String passWord2
                                     ){
        if (StringUtils.isBlank(userName)) {
            return ResponseResult.error(-1,"用户名不能为空");
        }
        if (StringUtils.isBlank(passWord)) {
            return ResponseResult.error(-1,"密码不能为空");
        }
        if (StringUtils.isBlank(passWord2)) {
            return ResponseResult.error(-1,"确认密码不能为空");
        }
        if (! passWord.equals(passWord2) ) {
            return ResponseResult.error(-1,"用户名不能为空");
        }else {
            int res = userService.addUser(userName, passWord);
            if (res == 0){
                return ResponseResult.error(-1,"注册失败");
            }else {
                return ResponseResult.success();
            }

        }
    }

}
